using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;


namespace MyFrame.UI
{
    /// <summary>
    /// 面板基类，提供淡入淡出 同时提供控件
    /// </summary>
    public abstract class BaseFadePanel : BasePanel
    {
        //整体控制淡入淡出的画布组
        public CanvasGroup canvasGroup;
        //淡入速度
        public float alphaSpeed = 10f;

        private bool isShow = false;

        //淡出成功时委托
        private UnityAction hideCallback;

        private Dictionary<string, List<UIBehaviour>> controDic = new();

        // Start is called before the first frame update
        void Awake()
        {
            canvasGroup = GetComponent<CanvasGroup>();
            if (canvasGroup == null)
                canvasGroup = gameObject.AddComponent<CanvasGroup>();
        }


        public virtual void Show()
        {
            isShow = true;
            canvasGroup.alpha = 0f;
        }

        public virtual void Hide(UnityAction callback = null)
        {
            isShow = false;
            canvasGroup.alpha = 1f;
            hideCallback = callback;
        }

        // todo 改成dotween
        void Update()
        {
            //控制淡入淡出的动画
            if (isShow && canvasGroup.alpha != 1f)
            {
                canvasGroup.alpha += alphaSpeed * Time.deltaTime;
                if (canvasGroup.alpha >= 1f)
                    canvasGroup.alpha = 1f;

            }
            else if (!isShow)
            {
                canvasGroup.alpha -= alphaSpeed * Time.deltaTime;
                if (canvasGroup.alpha <= 0f)
                {
                    canvasGroup.alpha = 0f;
                    //让管理器删除自己
                    hideCallback?.Invoke();
                }
            }
        }
    }

}